/*
 * @Author: LiuYan
 * @Date: 2022-10-28 10:01:41
 * @LastEditors: LiuYan
 * @LastEditTime: 2023-01-10 15:10:38
 * @Description:
 */
import { createRouter, createWebHashHistory } from "vue-router";
import * as app from "../config/index";
import { userStore } from "../store/user";
const { APP } = app.default;
const routes = [
  {
    path: "/",
    redirect: "/backend/home",
  },
  {
    path: "/backend/home",
    name: "home",
    component: () => import("../view/backend/home/index.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../view/login.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const user = userStore();
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = APP.title;
  }

  if (APP.isLogin) {
    if (to.fullPath === "/login") {
      next();
    } else {
      if (user.token && user.token.length) {
        next();
      } else {
        next("/login");
      }
    }
  } else {
    next();
  }
});
export default router;
